package com.sjsu.cloudmanager.client.ui;

import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.*;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.*;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.layout.*;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.user.client.Element;
import com.sjsu.cloudmanager.client.ui.model.MachineTreeBean;
import com.sjsu.cloudmanager.client.ui.model.LabTreeBean;

/**
 * Created by IntelliJ IDEA.
 * User: ckempaiah
 * Date: 9/22/11
 * Time: 11:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class LabMainPanel extends LayoutContainer {

    @Override
    protected void onRender(Element parent, int index) {
        super.onRender(parent, index);
        setBorders(false);
        TabPanel managerAppPanel = new TabPanel();
        managerAppPanel.setAutoWidth(true);
        managerAppPanel.setAutoHeight(true);
        managerAppPanel.setResizeTabs(true);

        TabItem manageLab = new TabItem("Manage Labs");
        //manageLab.addStyleName("pad-text");
        managerAppPanel.add(manageLab);


//        BorderLayout borderLayout = new BorderLayout();
        LabGridStore gridStore = new LabGridStore();
//        ContentPanel west = new ContentPanel();


//        BorderLayoutData westLayout = new BorderLayoutData(Style.LayoutRegion.WEST, 200);
//        westLayout.setSplit(true);
//        westLayout.setMargins(new Margins(0, 0, 5, 0));
//
//        BorderLayoutData centerData = new BorderLayoutData(Style.LayoutRegion.CENTER);
//        centerData.setMargins(new Margins(5, 0, 5, 0));
        //manageLab.setLayout(borderLayout);
//        manageLab.add(west, westLayout);
//        manageLab.add(gridStore, centerData);

        managerAppPanel.add(manageLab);


        //treePanel.setCollapsible(true);
        //treePanel.setHideCollapseTool(true);

//        ContentPanel cp  = new ContentPanel();
//        cp.setFrame(true);
//        cp.setLayout(new ColumnLayout());
//        cp.add(treePanel, new ColumnData(.3));
//        cp.add(gridStore, new ColumnData(.7));
//        manageLab.add(cp);
//        manageLab.setLayout(new ColumnLayout());
//        manageLab.add(treePanel, new ColumnData(.3));
//        manageLab.add(gridStore, new ColumnData(.7));
        manageLab.setBorders(false);

        BorderLayoutData westData = new BorderLayoutData(Style.LayoutRegion.WEST, 200);
        westData.setSplit(true);
        westData.setCollapsible(true);
        westData.setMargins(new Margins(0, 0, 0, 0));

        ContentPanel cp = new ContentPanel();
        cp.setFrame(true);
        cp.setLayout(new BorderLayout());

        BorderLayoutData centerData = new BorderLayoutData(Style.LayoutRegion.CENTER);
        centerData.setMargins(new Margins(0, 0, 0, 0));
        //centerData.setMargins(new Margins(5, 0, 5, 0));
        cp.setLayout(new BorderLayout());
        cp.add(getTreePanel(), westData);
        cp.add(gridStore, centerData);
        cp.setHeight(400);
        cp.setBorders(false);
        cp.setHeaderVisible(false);
        manageLab.add(cp);
        //add(treePanel, westData);
        //add(gridStore, centerData);
        add(managerAppPanel);

        //setLayout(new FitLayout());

    }

    private ContentPanel getTreePanel() {


        TreeStore<ModelData> store = new TreeStore<ModelData>();
        store.add(getTreeModel().getChildren(), true);

        final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);
        tree.setWidth(300);
        tree.setDisplayProperty("name");
        //tree.getStyle().setLeafIcon(Resources.ICONS.music());

        tree.addListener(Events.Select, new Listener<TreePanelEvent<ModelData>>() {
            @Override
            public void handleEvent(TreePanelEvent<ModelData> be) {
                MessageBox.alert("Sample Item", be.getItem().toString(), null);
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        ButtonBar buttonBar = new ButtonBar();

//        buttonBar.add(new Button("Expand All", new SelectionListener<ButtonEvent>() {
//            public void componentSelected(ButtonEvent ce) {
//                tree.expandAll();
//            }
//        }));
//        buttonBar.add(new Button("Collapse All", new SelectionListener<ButtonEvent>() {
//            public void componentSelected(ButtonEvent ce) {
//                tree.collapseAll();
//            }
//        }));
        ContentPanel treePanel = new ContentPanel();
        treePanel.setFrame(true);
        treePanel.add(buttonBar, new FlowData(10));
        treePanel.add(tree, new FlowData(10));

        return treePanel;
    }

    public LabTreeBean getTreeModel() {
        LabTreeBean[] treeBeanArray = new LabTreeBean[]{
                new LabTreeBean(1, "lab1", new MachineTreeBean[]{
                        new MachineTreeBean(2, "machine1"),
                        new MachineTreeBean(3, "machine2")
                }),
                new LabTreeBean(4, "lab2", new MachineTreeBean[]{
                        new MachineTreeBean(5, "machine1"),
                        new MachineTreeBean(6, "machine2")
                })
        };
        LabTreeBean root = new LabTreeBean(1,"root", treeBeanArray);

        return root;
    }
}
